import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 单位转换验证测试
 * 确保 g、kg、克、千克 能正确转换成吨/千吨
 */
public class UnitConversionVerification {
    
    public static void main(String[] args) {
        System.out.println("=== 单位转换完整验证 ===");
        
        // 测试 kg 转换
        testKgConversion();
        
        // 测试 g 转换
        testGConversion();
        
        // 测试中文单位转换
        testChineseUnits();
        
        // 测试完整流程
        testCompleteFlow();
        
        // 测试边界情况
        testBoundaryConditions();
    }
    
    /**
     * 测试 kg 转换
     */
    private static void testKgConversion() {
        System.out.println("\n--- kg 转换测试 ---");
        
        // 基础转换测试
        testConversion("1", "kg", "0.001000", "0.00吨");
        testConversion("10", "kg", "0.010000", "0.01吨");
        testConversion("100", "kg", "0.100000", "0.10吨");
        testConversion("1000", "kg", "1.000000", "1.00吨");
        testConversion("1500", "kg", "1.500000", "1.50吨");
        
        // 您的实际数据
        testConversion("672669", "kg", "672.669000", "672.67千吨");
        testConversion("686", "kg", "0.686000", "0.69吨");
        testConversion("37", "kg", "0.037000", "0.04吨");
        
        // 大数值测试
        testConversion("1000000", "kg", "1000.000000", "1.00千吨");
        testConversion("2500000", "kg", "2500.000000", "2.50千吨");
    }
    
    /**
     * 测试 g 转换
     */
    private static void testGConversion() {
        System.out.println("\n--- g 转换测试 ---");
        
        // 基础转换测试
        testConversion("1", "g", "0.000001", "0.00吨");
        testConversion("100", "g", "0.000100", "0.00吨");
        testConversion("1000", "g", "0.001000", "0.00吨");
        testConversion("10000", "g", "0.010000", "0.01吨");
        testConversion("100000", "g", "0.100000", "0.10吨");
        testConversion("1000000", "g", "1.000000", "1.00吨");
        
        // 您的实际数据
        testConversion("119", "g", "0.000119", "0.00吨");
        
        // 大数值测试
        testConversion("1000000000", "g", "1000.000000", "1.00千吨");
        testConversion("2500000000", "g", "2500.000000", "2.50千吨");
    }
    
    /**
     * 测试中文单位
     */
    private static void testChineseUnits() {
        System.out.println("\n--- 中文单位转换测试 ---");
        
        // 千克转换
        testConversion("672669", "千克", "672.669000", "672.67千吨");
        testConversion("1000", "千克", "1.000000", "1.00吨");
        testConversion("500", "千克", "0.500000", "0.50吨");
        
        // 克转换
        testConversion("119", "克", "0.000119", "0.00吨");
        testConversion("1000000", "克", "1.000000", "1.00吨");
        testConversion("500000", "克", "0.500000", "0.50吨");
    }
    
    /**
     * 测试完整流程
     */
    private static void testCompleteFlow() {
        System.out.println("\n--- 完整流程测试 ---");
        
        System.out.println("模拟您的实际数据处理：");
        
        // 原始数据
        BigDecimal paper = new BigDecimal("672669");    // kg
        BigDecimal clothes = new BigDecimal("686");     // kg  
        BigDecimal plastic = new BigDecimal("37");      // kg
        BigDecimal metal = new BigDecimal("119");       // g
        
        // 转换为吨
        BigDecimal paperTons = convertToStandardUnit(paper, "kg");
        BigDecimal clothesTons = convertToStandardUnit(clothes, "kg");
        BigDecimal plasticTons = convertToStandardUnit(plastic, "kg");
        BigDecimal metalTons = convertToStandardUnit(metal, "g");
        
        // 格式化显示
        String paperDisplay = formatValueWithUnit(paperTons, "kg");
        String clothesDisplay = formatValueWithUnit(clothesTons, "kg");
        String plasticDisplay = formatValueWithUnit(plasticTons, "kg");
        String metalDisplay = formatValueWithUnit(metalTons, "g");
        
        System.out.println("纸品类: " + paper + "kg -> " + paperTons + "吨 -> " + paperDisplay);
        System.out.println("旧衣类: " + clothes + "kg -> " + clothesTons + "吨 -> " + clothesDisplay);
        System.out.println("塑料类: " + plastic + "kg -> " + plasticTons + "吨 -> " + plasticDisplay);
        System.out.println("贵重金属: " + metal + "g -> " + metalTons + "吨 -> " + metalDisplay);
        
        // 计算总和
        BigDecimal total = paperTons.add(clothesTons).add(plasticTons).add(metalTons);
        String totalDisplay = formatValueWithUnit(total, "kg");
        System.out.println("总计: " + total.setScale(6, RoundingMode.HALF_UP) + "吨 -> " + totalDisplay);
    }
    
    /**
     * 测试边界情况
     */
    private static void testBoundaryConditions() {
        System.out.println("\n--- 边界情况测试 ---");
        
        // 接近1000吨的情况
        testConversion("999990", "kg", "999.990000", "999.99吨");
        testConversion("999995", "kg", "999.995000", "1000.00吨");
        testConversion("1000000", "kg", "1000.000000", "1.00千吨");
        testConversion("1000005", "kg", "1000.005000", "1.00千吨");
        
        // 很小的数值
        testConversion("1", "g", "0.000001", "0.00吨");
        testConversion("999", "g", "0.000999", "0.00吨");
        testConversion("1001", "g", "0.001001", "0.00吨");
        
        // 零值
        testConversion("0", "kg", "0.000000", "0.00吨");
        testConversion("0", "g", "0.000000", "0.00吨");
    }
    
    /**
     * 测试单个转换
     */
    private static void testConversion(String inputStr, String unit, String expectedTons, String expectedDisplay) {
        BigDecimal input = new BigDecimal(inputStr);
        BigDecimal resultTons = convertToStandardUnit(input, unit);
        String resultDisplay = formatValueWithUnit(resultTons, unit);
        
        boolean tonsCorrect = resultTons.setScale(6, RoundingMode.HALF_UP).toString().equals(expectedTons);
        boolean displayCorrect = resultDisplay.equals(expectedDisplay);
        
        String status = (tonsCorrect && displayCorrect) ? "✓" : "✗";
        System.out.println(status + " " + inputStr + unit + " -> " + 
                          resultTons.setScale(6, RoundingMode.HALF_UP) + "吨 -> \"" + resultDisplay + "\"");
        
        if (!tonsCorrect) {
            System.out.println("    ❌ 吨转换错误！期望: " + expectedTons + ", 实际: " + resultTons.setScale(6, RoundingMode.HALF_UP));
        }
        if (!displayCorrect) {
            System.out.println("    ❌ 显示错误！期望: \"" + expectedDisplay + "\", 实际: \"" + resultDisplay + "\"");
        }
    }
    
    /**
     * 转换为标准单位（吨）
     */
    private static BigDecimal convertToStandardUnit(BigDecimal value, String unit) {
        if (value == null) {
            return BigDecimal.ZERO;
        }
        
        String unitLower = unit.toLowerCase().trim();
        switch (unitLower) {
            case "kg":
            case "千克":
                return value.divide(new BigDecimal("1000"), 6, RoundingMode.HALF_UP);
            case "g":
            case "克":
                return value.divide(new BigDecimal("1000000"), 6, RoundingMode.HALF_UP);
            case "吨":
            case "t":
            default:
                return value;
        }
    }
    
    /**
     * 格式化值并添加单位
     */
    private static String formatValueWithUnit(BigDecimal value, String originalUnit) {
        if (value == null) {
            value = BigDecimal.ZERO;
        }
        
        String unitLower = originalUnit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
            case "kg":
            case "千克":
            case "g":
            case "克":
                // 重量类型：根据大小决定显示吨还是千吨
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
                    BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
                    return weightInKiloTons.toString() + "千吨";
                } else {
                    return formattedWeight.toString() + "吨";
                }
            default:
                return value.setScale(2, RoundingMode.HALF_UP).toString();
        }
    }
}
